var mydialog;
$(function(){
	$("#chk_de_1").click(function(){
		 if($(this).attr("checked")){
			 $("#selected_rent").show();
		 }else{
			 $("#selected_rent").hide();
		 }
	 });
	 $("#chk_de_2").click(function(){
		 if($(this).attr("checked")){
			 $("#selected_sell").show();
		 }else{
			 $("#selected_sell").hide();;
		 }
	 });
	$("#icon-plus").show();
	$("#icon-minus").hide();
	$(".save").button();
});

function checkform(){
	
	var check=$(".check");
	var error=0;
	var ischecked=0;
	$.each(check,function(key,value){
		if(value["value"]==''||value["value"]==undefined){
			alert($(this).attr("info")+"不能为空");
			error++;
			return false;
		}else if(value["type"]=='checkbox'){
			if(value['checked']){
				ischecked++;
			}
		}else if(value["type"]=='radio'){//修改頁面 
			if(value['checked']){
				ischecked++;
			}
		}
	});
	
	if(error!=0){
		return;
	}
	
	if(ischecked==0){
		alert("請選擇需求類型");
		return;
	}
	
	if(error==0){
		$("#form1").submit();
	}
}

function checkNameAndDemand(){
	var name=$("#cu_name").val();
	var chk1 = $("#chk_de_1").attr('checked');
	var chk2 = $("#chk_de_2").attr('checked');
	var chk3 = $("#chk_de_3").attr('checked');
	var demand = "";
	if(chk1 && !chk2){
		demand = '1';
	}else if(chk2 && !chk1){
		demand = '2';
	}else if(chk1 && chk2){
		demand = '1,2'
	}else if(chk3){
		demand = '3';
	}
	//alert(name+demand);
	$.ajax({
		url:'checkNameAndDemand',
		type:'post',
		dataTyep:'html',
		data:{name:name,demand:demand},
		success:function(data){
			if(data.status===1){
				alert("需求類型+姓名不可重復");
			}else if(data.status===2){
				alert("ok");
			}
		}
	});
}

function showFamilyAddress(){
	$.ajax({
		url:'FamilyAddress',
		type:'post',
		Global:false,
		dataType:'html',
		success:function(data){
			art.dialog({
				title:'地址',
				content:data,
				lock:true,
				ok:function(){},
				cancel:function(){}
			});
		}
	});
}

function demand_save_status(data){
	mydialog.close();
	if(data.status===0){
		art.dialog({lock:true,title:'提示',content:'没有权限',ok:function(){}});
	}else if(data.status===2){
		art.dialog({lock:true,title:'提示',content:'非法訪問',ok:function(){}});
	}else if(data.status===3){
		art.dialog({lock:true,title:'提示',content:data.info,ok:function(){}});
	}else if(data.status===1){
		art.dialog({lock:true,title:'提示',content:'存檔完成',ok:function(){
			location.reload();
		}});
	}
}

function getTimes(){
	var d = new Date()
	var year = d.getFullYear();
	var month = d.getMonth() + 1;
	var months=""+month;
	if(month<10){
		months="0"+month;
	}
	var day = d.getDate();
	var days=""+day;
	if(day<10){
		days="0"+day;
	}
	return year+'-'+months+'-'+days;
}

function show_userinfo(){
	art.dialog({
		width:600,
		height:320,
		title:'選擇客戶',
		lock:true,
		content:"<iframe src='"+actionurl+"/userinfo' class='userinfo_iframe' name='userinfo_iframe'  ></iframe>",
		ok:function(){
			var value=$(window.frames["userinfo_iframe"].document).find("#showuserinfo input:radio[name='userinfo']:checked").val();
			if(value==undefined){
				alert('請選擇客戶');
				return false;
			}else{
				var cu_name=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'1').html();
				var u_mo=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'2').html();
				var u_htel=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'3').html();
				var u_ctel=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'4').html();
				var u_mail=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'5').html();
				var u_hadd=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'6').html();
				var u_cadd=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'9').html();
				var u_impor=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'10').html();
				var b_name=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'7').html();
				var c_name=$(window.frames["userinfo_iframe"].document).find("#showuserinfo #"+value+'8').html();
				
				$('#cu_name').val(cu_name);
				$('#cu_mobile').val(u_mo);
				$('#cu_htel').val(u_htel);
				$('#cu_ctel').val(u_ctel);
				$('#cu_mail').val(u_mail);
				$('#cu_hadd').val(u_hadd);
				$('#cu_cadd').val(u_cadd);
				$('#cu_impor').val(u_impor);
				$('#cu_hbuild').val(b_name);
				$('#cu_company').val(c_name);
			}
		},
		cancel:function(){}
	});
}

//加載捷運站
function getLineData(obj,type){
	var line=$(obj).find("option:selected").html();
	$.ajax({
		url:'getLineData',
		type:'post',
		data:{line:line},
		dataType:'json',
		success:function(data){
			getLineData_status(data,type);
		}
	});
}

function getLineData_status(data,type){
	if(data.status===1){
		if(type==1){
			var options='';
			$.each(data.data,function(key,value){
				options+="<option value='"+value['id']+"'>"+value['ba_mrt']+"</option>";
			});
			$('#ba_mrt1').html(options);
			$('#ba_mrt2').html(options);
		}else if(type==2){
			var options='';
			$.each(data.data,function(key,value){
				options+="<option value='"+value['id']+"'>"+value['ba_mrt']+"</option>";
			});
			$('#ba_mrt3').html(options);
			$('#ba_mrt4').html(options);
		}
	}else if(data.status===2){
		alert('加載捷運站失敗');
	}
}
//捷運站  END

//school start
function showSchool(id){
	mydialog=art.dialog({
		title:'選擇學校',
		lock:true,
		ok:function(){
			$("#"+id).val($("#address").val());
		},
		cancel:function(){}
	});
	$.ajax({
		url:'school',
		type:'post',
		Global:false,
		dataType:'html',
		success:function(data){
			mydialog.content(data);
		}
	});
}

function city_select2(obj){
	var id=$(obj).attr("value");
	var table="address_county";
	var value=$('#city option:selected').text();
	$("#address").val(value);
	if(id!=''){
		$("#school_type").val(0);
	}
}

function s_type_selected(obj){
	var value=$('#city option:selected').text()+$('#school_type option:selected').text();
	$("#address").val(value);
	var c1=$('#city option:selected').text();
	var c2=$('#school_type option:selected').text();
	if(c1!=''&&c2!=''&&c1!=undefined&&c2!=undefined){
		$("#school_name").html("");
		$.ajax({
			url:actionurl+'/school',
			type:'post',
			dataType:'json',
			data:{c1:c1,c2:c2,type:1},
			success:s_type_selected_status,
			error:function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus);}
		});
	}
}

function s_type_selected_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['id']+"' >"+value['ba_school']+"</option>";
		});
		$("#school_name").html(options);
	}else if(data.status===2){
		alert(data.data);
	}
}

function school_selected(obj){
	var value=$('#school_name option:selected').text();
	$("#address").val(value);
}

//school end

//park start
function showPark(id){
	mydialog=art.dialog({
		title:'選擇公園',
		width:460,
		lock:true,
		ok:function(){
			$("#"+id).val($("#address").val());
		},
		cancel:function(){}
	});
	$.ajax({
		url:'park',
		type:'post',
		Global:false,
		dataType:'html',
		success:function(data){
			mydialog.content(data);
		}
	});
}

function city_select_park(obj){
	var city = $('#city').val();
	if(city!='' && city!=undefined){
		$('#country').html("");
		$("#park").html("");
		$.ajax({
			url:actionurl+'/park',
			type:'post',
			dataType:'json',
			data:{city:city,type:1},
			success:city_selected_status,
			error:function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus);}
		});
	}
}
function city_selected_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['ba_area']+"' >"+value['ba_area']+"</option>";
		});
		$("#country").html(options);
	}else if(data.status===2){
		alert(data.data);
	}
}

function country_selected_park(obj){
	var value=$('#city option:selected').text()+$('#country option:selected').text();
	$("#address").val(value);
	var c1=$('#city').val();
	var c2=$('#country').val();
	if(c1!=''&&c2!=''&&c1!=undefined&&c2!=undefined){
		$("#park").html("");
		$.ajax({
			url:actionurl+'/park',
			type:'post',
			dataType:'json',
			data:{c1:c1,c2:c2,type:2},
			success:country_selected2_status,
			error:function(XMLHttpRequest, textStatus, errorThrown){alert(textStatus);}
		});
	}
}

function country_selected2_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['id']+"' >"+value['ba_park']+"</option>";
		});
		$("#park").html(options);
	}else if(data.status===2){
		alert(data.data);
	}
}

function park_selected(obj){
	var value=$('#park option:selected').text();
	$("#address").val(value);
}

//park end

function de_resize(obj){
	$(obj).css("position","absolute");
	$(obj).css("width","250px");
}

function de_reset(obj){
	$(obj).css("position","");
	$(obj).css("width","");
}

function peidui(title,fields){
    var demand_t = '';
	$(':checkbox:checked').each(function(i){
		if($(this).val())
			demand_t += $(this).val() + ',';
	});	
	ba_use = $("#ba_use").val();
    de_buil1 = $("#de_buil1").val();
    de_buil2 = $("#de_buil2").val();
    ba_curre = $("#ba_curre").val();
    de_park1 = $("#de_park1").val();
    de_land1 = $("#de_land1").val();
    de_land2 = $("#de_land2").val();
    //ba_area1 = $("#ba_area1").val();
    //ba_area2 = $("#ba_area2").val();
    ba_attac1 = $("#ba_attac1").val();
    ba_attac2 = $("#ba_attac2").val(); 
    ob_case1 = $("#ob_case1").val();
    ob_case2 = $("#ob_case2").val();
    ba_park1 = $("#ba_park1").val();
    ba_park2 = $("#ba_park2").val();
    ba_distr1 = $("#ba_distr1").val();
    ba_distr2 = $("#ba_distr2").val();
    de_admarea = $("#de_admarea").val(); 
    de_admarea1 = $("#de_admarea1").val();  
    de_year1 = $("#de_year1").val();
    de_obj = $("#de_obj").val();
    ba_floor = $("#ba_floor").val(); 
    de_floor1 = $("#de_floor1").val();
    de_floor2 = $("#de_floor2").val(); 
    de_heig = $("#de_heig").val();  
    ba_located1 = $("#ba_located1").val(); 
    ba_located2 = $("#ba_located2").val();  
    ba_styl = $("#ba_styl").val();  
    ba_public = $("#ba_public").val();
    ba_subs = $("#ba_subs").val();  
    ba_mana = $("#ba_mana").val();  
    de_part = $("#de_part").val();  
    ba_curre = $("#ba_curre").val();  
    ba_unit = $("#ba_unit").val();
    
    $.ajax({
		type:"post",
		url:"pipei_search",
		dataType:"json",
		data:{ba_use:ba_use,demand_t:demand_t,de_buil1:de_buil1,de_buil2:de_buil2,de_park1:de_park1,de_land1:de_land1,de_land2:de_land2,ba_attac1:ba_attac1,ba_attac2:ba_attac2,ob_case1:ob_case1,ob_case2:ob_case2,ba_park1:ba_park1,ba_park2:ba_park2,ba_distr1:ba_distr1,ba_distr2:ba_distr2,de_admarea:de_admarea,de_admarea1:de_admarea1,de_year1:de_year1,de_obj:de_obj,ba_floor:ba_floor,de_floor1:de_floor1,de_floor2:de_floor2,de_heig:de_heig,ba_located1:ba_located1,ba_located2:ba_located2,ba_styl:ba_styl,ba_public:ba_public,ba_subs:ba_subs,ba_mana:ba_mana,de_part:de_part,ba_curre:ba_curre,ba_unit:ba_unit,ba_curre:ba_curre},
		success:function(data){
			if(data.status == 3){
				alert("查無配對結果");
			}else if(data.status==2){  // 物件编号
//				var table = "<input type='button' value='正式配對' onclick='zs_peidui();'/>";
//				table += '<table style="border-top:1px solid #000;border-left:1px solid #000;"><tr><th style="border-right:1px solid #000;border-bottom:1px solid #000;">物件編號/<br>市調編號</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">物件狀態/<br>市調狀態</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">案名</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">房屋售價/<br>房屋租金</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">主建物面積</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;width:90px;">附屬建物面積</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">公設</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">車位</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">每月管理費</th>';
//				$.each(data.data,function(key,value){
//					table = table +'<tr><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_no"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_type1"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["cu_hbuild"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_sell1"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_mainbuil"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_subsid"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_post"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_parkspac"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_monfee"]+'</td></tr>';
//				});
//				table = table+"</tbody>";
//				table = table+"</table>";
				var table = data.data;
				mydialog=art.dialog({
					width:800,
					height:300,
					title:title,
					content:table				
				});
			}			
		}		
	});
	
} 

//路段 start
function showRoadTr(){
	$("#roadTr").show();
	$("#icon-minus").show();
	$("#icon-plus").hide();		
}
function hideRoadTr(){
	$("#roadTr").hide();
	$("#icon-plus").show();
	$("#icon-minus").hide();
}
//路段 end

//添加指定樓層
function show_prompt(){
	var val = prompt("請輸入樓層數,多個時以半角逗號隔開！",$('#de_floor').val());
	if(val != null && val != ''){
		$('#de_floor').val(val);
	}
}

function zs_peidui(){
	var de_map = $("#de_map").val();
	var ob_ids = $("#ob_ids").val();
	//alert(de_map);
	//alert(ob_ids);
	$.ajax({
		type:"post",
		url:'zs_peidui',
		dataType:"json",
		data:{de_map:de_map,ob_ids:ob_ids},
		success:function(data){
			if(data.status == 1){
				//$('#zs_btn').css('display','none');
				alert('配对成功');				
			}else{
				alert('操作失败');
			}
		}
	});
}



